package com.asset.dispoal.file.casedispose.service.command;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.entity.CaseStateRecord;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import com.asset.dispoal.file.casedispose.dto.FileCaseJudgmentRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.Date;

@Component
public class FileCaseJudgmentCommand implements BaseCommand<FileCaseJudgmentRequest, Result> {

    @Autowired
    FileCaseDisposeSaveFileCommand fileCaseDisposeSaveFileCommand;

    @Autowired
    BsCaseMapper caseMapper;

    @Autowired
    BaseIdeable<String> ideable;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public synchronized Result execute(FileCaseJudgmentRequest request) {
        String caseState = AgreementCaseSateMenu.JUDGMENT.getLabel();
        String userId = GlobalHeaderThreadLocal.get().getUserId();
        Date currentDate = DateTimeUtil.currentDate();
        if (request.getJudgmentFile() != null) {
            fileCaseDisposeSaveFileCommand.execute(request.getId(), request.getJudgmentFile(), caseState, "verdictFile");
        }

        BsCase bsCase = new BsCase();
        bsCase.setId(request.getId());
        bsCase.setCaseState(caseState);
        bsCase.setOperateUser(userId);
        bsCase.setOperateTime(currentDate);
        bsCase.setJudgmentTime(request.getJudgmentTime());
        bsCase.setPrincipal(request.getPrincipal());
        bsCase.setInterest(request.getInterest());
        bsCase.setPenaltyInterest(request.getPenaltyInterest());
        bsCase.setCaseAcceptAmount(request.getCaseAcceptAmount());
        bsCase.setJudgmentPreservationAmount(request.getJudgmentPreservationAmount());
        bsCase.setBaseNumber1(request.getBaseNumber1());
        bsCase.setBaseNumber2(request.getBaseNumber2());
        bsCase.setBaseNumber3(request.getBaseNumber3());
        bsCase.setBaseNumber4(request.getBaseNumber4());
        bsCase.setBaseNumber5(request.getBaseNumber5());
        bsCase.setJudgmentType1(request.getJudgmentType1());
        bsCase.setJudgmentType2(request.getJudgmentType2());
        bsCase.setJudgmentType3(request.getJudgmentType3());
        bsCase.setJudgmentType4(request.getJudgmentType4());
        bsCase.setJudgmentType5(request.getJudgmentType5());
        bsCase.setJudgmentRate1(request.getJudgmentRate1());
        bsCase.setJudgmentRate2(request.getJudgmentRate2());
        bsCase.setJudgmentRate3(request.getJudgmentRate3());
        bsCase.setJudgmentRate4(request.getJudgmentRate4());
        bsCase.setJudgmentRate5(request.getJudgmentRate5());
        bsCase.setJudgmentCalculationTime1(request.getJudgmentCalculationTime1());
        bsCase.setJudgmentCalculationTime2(request.getJudgmentCalculationTime2());
        bsCase.setJudgmentCalculationTime3(request.getJudgmentCalculationTime3());
        bsCase.setJudgmentCalculationTime4(request.getJudgmentCalculationTime4());
        bsCase.setJudgmentCalculationTime5(request.getJudgmentCalculationTime5());
        bsCase.setJudgmentDay1(request.getJudgmentDay1());
        bsCase.setJudgmentDay2(request.getJudgmentDay2());
        bsCase.setJudgmentDay3(request.getJudgmentDay3());
        bsCase.setJudgmentDay4(request.getJudgmentDay4());
        bsCase.setJudgmentDay5(request.getJudgmentDay5());
        bsCase.setBrief(request.getBrief());
        bsCase.setJudge(request.getJudge());
        caseMapper.judgment(bsCase);
        CaseStateRecord caseStateRecord = new CaseStateRecord();
        caseStateRecord.setId(ideable.generateId());
        caseStateRecord.setOldState(AgreementCaseSateMenu.REGISTER_CASE.getLabel());
        caseStateRecord.setNewState(caseState);
        caseStateRecord.setCaseId(request.getId());
        caseStateRecord.setCreateUser(userId);
        caseMapper.insertStateRecord(caseStateRecord);
        return Result.ok();
    }
}
